package com.audaque.springboot.foshanupload.core.util;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zgb
 * @desc 并发测试工具
 * @date 2022-05-09 11:12:18
 */
@Slf4j
public class ConcurrenceTestUtils {
    @SneakyThrows
    public static void concurrenceTest() {
    //并发数
    final int count= 1000;
    //任务倒数
    final CountDownLatch countDownLatch = new CountDownLatch(count);
    //业务执行倒数
    final CountDownLatch countDownLatch2 = new CountDownLatch(count);
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 1000; i++) {
        executorService.submit(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                //一直阻塞当前线程，直到计时器的值为0,保证同时并发
                countDownLatch.await();
                log.debug("开始并发执行任务");
                //执行业务
                //business();
                //业务执行倒数
                countDownLatch2.countDown();
            }
        });
        //任务倒数
        countDownLatch.countDown();
    }
    //一直阻塞当前线程，直到计时器的值为0,保证同时并发
    countDownLatch2.await();
    log.debug("所有任务执行结束");
    executorService.shutdown();
}

}
